<#include "*/component/CombineFieldTags.ftl"/>
<@Title/>
<@ClassAnnotation>
@Repository
</@ClassAnnotation>
public class ${clazz.name} implements ${clazz.parent.name} {

${'\t'}${entity.entityName}Converter ${entity.objectName}Converter = ${entity.entityName}Converter.instance;

${'\t'}@Autowired
${'\t'}private ${entity.entityName}Dao ${entity.objectName}Dao;

${'\t'}@Override
${'\t'}public ${entity.entityName} find(Long id) {
${'\t'}${'\t'}${entity.entityName}Entity entity = this.${entity.objectName}Dao.getOne(new ${entity.entityName}Entity.IDCondition(id), ${entity.entityName}Entity.class);
${'\t'}${'\t'}return ${entity.objectName}Converter.domain(entity);
${'\t'}}

${'\t'}@Override
${'\t'}public ${entity.entityName} forceFind(Long id) {
${'\t'}${'\t'}${entity.entityName} ${entity.objectName} = this.find(id);
${'\t'}${'\t'}if (${entity.objectName} == null) {
${'\t'}${'\t'}${'\t'}throw new BaseException("${entity.table.comment}不存在");
${'\t'}${'\t'}}
${'\t'}${'\t'}return ${entity.objectName};
${'\t'}}

${'\t'}@Override
${'\t'}public void store(${entity.entityName} ${entity.objectName}) {
${'\t'}${'\t'}Long id = ${entity.objectName}.getId();
${'\t'}${'\t'}Date now = new Date(System.currentTimeMillis());
${'\t'}${'\t'}if (IDUtils.isNull(id)){
${'\t'}${'\t'}${'\t'}${entity.entityName}DO entity = ${entity.objectName}Converter.dataObject(${entity.objectName});
${'\t'}${'\t'}${'\t'}entity.setCreateTime(now);
${'\t'}${'\t'}${'\t'}entity.setUpdateTime(now);
<#--组合字段-->
<#list entity.fields as field >
    <#if field.column.isCombine>
${'\t'}${'\t'}${'\t'}entity.set${field.capitalizeName}(<#if field.combineFields?size == 1>ColumnUtils.getDeletableUniqueName(<@HyperCombineValue field=field prefix='${entity.objectName}.get' suffix='()' separator=',' />)<#else>ColumnUtils.combineColumns(<@HyperCombineValue field=field prefix='${entity.objectName}.get' suffix='()' separator=',' />)</#if>);
    </#if>
</#list>
${'\t'}${'\t'}${'\t'}${entity.objectName}Dao.save(entity);
${'\t'}${'\t'}${'\t'}${entity.objectName}.setId(entity.getId());
${'\t'}${'\t'}}else {
${'\t'}${'\t'}${'\t'}${entity.entityName}Entity entity = ${entity.objectName}Converter.entity(${entity.objectName});
${'\t'}${'\t'}${'\t'}entity.setUpdateTime(now);
<#--组合字段-->
<#list entity.fields as field >
    <#if field.column.isCombine>
${'\t'}${'\t'}${'\t'}entity.set${field.capitalizeName}(<#if field.combineFields?size == 1>ColumnUtils.getDeletableUniqueName(<@HyperCombineValue field=field prefix='${entity.objectName}.get' suffix='()' separator=',' />)<#else>ColumnUtils.combineColumns(<@HyperCombineValue field=field prefix='${entity.objectName}.get' suffix='()' separator=',' />)</#if>);
    </#if>
</#list>
${'\t'}${'\t'}${'\t'}${entity.entityName}Entity.IDCondition updateCondition = new ${entity.entityName}Entity.IDCondition(id);
${'\t'}${'\t'}${'\t'}${entity.objectName}Dao.update(entity, updateCondition);
${'\t'}${'\t'}}
${'\t'}}

${'\t'}@Override
${'\t'}public void delete(Long id) {
${'\t'}${'\t'}${entity.entityName}DeleteCDO.DeleteCondition deleteCondition = new ${entity.entityName}DeleteCDO.DeleteCondition(id);
${'\t'}${'\t'}${entity.objectName}Dao.update(new ${entity.entityName}DeleteCDO(new Date(System.currentTimeMillis())), deleteCondition);
${'\t'}}
<#list entity.fields as field>
<#if field.column.uniqueKey>

${'\t'}@Override
${'\t'}public ${entity.entityName} findBy${field.capitalizeName}(<@CombineParam field=field showTpye=true/>){
${'\t'}${'\t'}${entity.entityName}Criteria criteria = ${entity.entityName}Criteria.create().${field.name}(<@CombineParam field=field showTpye=false/>);
${'\t'}${'\t'}${entity.entityName}Entity target = this.${entity.objectName}Dao.getOne(criteria, ${entity.entityName}Entity.class);
${'\t'}${'\t'}return ${entity.objectName}Converter.domain(target);
${'\t'}}
</#if>
</#list>

}
